//检查登录 获取缓存
checkLogin();
var url = worker_order_list;
var page = 1;
var work_info = getCache(login_key);
var ref = GetQueryString('ref') || 0;
//-----------防止自动下拉触发分页请求处理所需变量 start-----------------
var flag = false;
var pull = false;
//-----------防止自动下拉触发分页请求处理所需变量 end-----------------
// 加载flag
var loading = false;
var is_more = true;
var minNum = 4;//最小数据长度
var dataNum = 0;
var orderType = function () {
    var value = 1;
    $('#order_type').children().each(function (i, v) {
        if ($(v).hasClass('active')) {
            value = $(v).attr('data');
        }
    });
    return value;
};
var typeArr = [['order', 'dingdan_pull_one'], ['take_inRoad', 'dingdan_pull_two'], ['done_inRoad', 'dingdan_pull_three']];

function addItems(type) {
    if (!loading && flag && pull) {
        flag = false;
        pull = false;
        return;
    }
    var type = type || orderType();
    if (ref) {
        type = ref;
        $('#t1').removeClass('active');
        $('#t'+ref).addClass('active');
        $('#tab1').removeClass('active');
        $('#tab'+ref).addClass('active');
        ref = 0;
    }
    var success = function (res) {
        var order_list = res.data.order_list;
        template.defaults.imports.shop_name = work_info.shop_name;
        if (order_list.next_page_url) {
            page += 1;
        } else {
            url = '';
            is_more = false;
            loading = false;
        }
        dataNum = res.data.order_list.data.length;
        var html = template(typeArr[type - 1][0], order_list);     //order 模板id
        // 添加新条目
        $('.infinite-scroll #' + typeArr[type - 1][1]).append(html);

        //点击跳转详情页面
        $(".order-content").bind('click', function () {
            var order_id = $(this).attr('data');
            if (type == 2) {
                redirect("order-detail.html?o_id=" + order_id);
            }
            if (type == 3) {
                redirect("order-done.html?o_id=" + order_id);
            }
        });

        //接单
        $(".button-dingdan").bind('click', function () {
            $(this).attr('disabled', 'true').css('background-color', 'lightgray');
            var id = $(this).attr('data');
            var success = function (res) {
                $("#list_" + id).hide();
            };
            var error = function (res) {
                setTimeout(function(){
                    $("button[disabled='disabled']").each(function(i,n){
                        $(n).removeAttr('disabled').css('background-color', 'rgba(253, 61, 0, 0.9)');
                    });
                },2000)
            };
            getData(worker_order_send, {
                aid: work_info.aid,
                sid: work_info.sid,
                worker_id: work_info.wid,
                id: id
            }, success, error);
        });


        //如果当前页是第一页且数据量不够滚动加载页面高度则删除加载符
        if (page == 1 && minNum > dataNum) {
            $('.infinite-scroll-preloader').hide();
        }

    };
    getData(url, {type: type, wid: work_info.wid, aid: work_info.aid, sid: work_info.sid, page: page}, success);
}

addItems();

$.init();
// 注册'infinite'事件处理函数
$(document).on('infinite', '.infinite-scroll', function () {
    // 如果正在加载，则退出
    if (loading) return;
    // 设置flag
    loading = true;
    // 模拟1s的加载过程
    setTimeout(function () {
        // 重置加载flag
        loading = false;
        pull = true;
        if (!is_more) {
            //删除提示符
            $('.infinite-scroll-preloader').hide();
            return;
        }
        addItems();
        //容器发生改变,如果是js滚动，需要刷新滚动
    }, 1000);
});
$('#order_type').children().click(function () {
    var self = $(this);
    var oldType = parseInt(orderType());
    var newType = parseInt(self.attr('data'));
    if (oldType != newType) {
        //加载提示符
        // addPreloader();
        $('.infinite-scroll-preloader').show();
        for (var i = 1; i <= typeArr.length; i++) {
            $('.infinite-scroll #' + typeArr[i - 1][1]).html('');
        }
        page = 1;
        url = worker_order_list;
        is_more = true;
        loading = false;
        addItems(newType)
        flag = true;
    }
});

$('#dingdan').click(function (event) {
    redirect('order-list.html');
    // $(this).find('#dingdan_img').attr('src', './image/dingdan-active.png');
    $(this).find('#daoke_img').attr('src', './image/daoke.png');
    $(this).find('#wode_img').attr('src', './image/wode.png');

    return false;
});
$('#daoke').click(function (event) {
    redirect('import.html');
    $(this).find('#dingdan_img').attr('src', './image/dingdan.png');
    // $(this).find('#daoke_img').attr('src', './image/daoke-active.png');
    $(this).find('#wode_img').attr('src', './image/wode.png');
    return false;
});
$('#wode').click(function (event) {
    redirect('center.html');
    $(this).find('#dingdan_img').attr('src', './image/dingdan.png');
    $(this).find('#daoke_img').attr('src', './image/daoke.png');
    // $(this).find('#wode_img').attr('src', './image/wode-active.png');
    return false;
});











